package com.shj.em.biz.sell.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.shj.em.biz.sell.entity.QuotItem;
import com.shj.em.biz.sell.mapper.QuotItemMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.plugins.Page;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.shj.em.biz.sys.service.CdService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


/**
 * 报价信息 服务实现类
 *
 * @author Shen Huang Jian
 * @since 2018-07-07
 */
@Service
@Transactional
public class QuotItemServiceImpl extends ServiceImpl<QuotItemMapper, QuotItem> implements QuotItemService {

    @Autowired
    QuotItemMapper quotitemMapper;

    @Autowired
    CdService cdService;

    public Page<QuotItem> queryQuotItemPage(Map<String, Object> params){
        int offset = (Integer)params.get("offset");
        int pageSize = (Integer)params.get("limit");
        int pageNo = offset / pageSize + 1;
        params.put("unitType", CdService.CD_TYPE_UNIT);
        Page<QuotItem> page = new Page(pageNo, pageSize);
        List<QuotItem> list = quotitemMapper.queryQuotItemPage(page, params);
        return page.setRecords(list);
    }

    public List<QuotItem> queryQuotItemsByQuotId(long quotId){
        Wrapper<QuotItem> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("quot_id", quotId);

        List<QuotItem> items = quotitemMapper.selectList(wrapper);
        for(QuotItem item : items){
            item.setUnitDescr(cdService.getDescrByTypeAndCd(CdService.CD_TYPE_UNIT, item.getUnit()));
        }
        return items;
    }

    public BigDecimal getTotalPriceByQuotId(Long quotId){
        return quotitemMapper.getTotalPriceByQuotId(quotId);
    }
}
